/*
 * Content: Java线程池
 * --------------------------------------------------
 * java 多线程: https://juejin.cn/post/6844903591044448270
 */

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class java_1102_threadPool {

    public static void main(String[] args) {

        // Java线程池
        System.out.println("--------------------Java线程池--------------------");

        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);

        int i = 0;

        while (i++ < 100) {
            final int index = i;

            // 最多就只会创建 5 个核心线程 - // newFixedThreadPool(5)
            fixedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
//                    if(index == 1){
//                        throw new IllegalStateException("handler exception");   //线程运行时抛出异常
//                    }

                    System.out.println(Thread.currentThread().getName() + "执行");

                    try {
                        Thread.sleep(1000);   //模拟当前线程执行其他操作 花费3s
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });

        }

        fixedThreadPool.shutdown();
    }
}
